﻿namespace Blaze.Editor.LuaExtensions
{
    using System.Collections.Generic;
    using System.IO;
    using EditorOnly;
    using Framework;
    using Framework.LuaExtensions;

    /// <summary>
    /// 框架默认提供的lua脚本源。
    /// </summary>
    [CustomScriptSource(typeof(LuaBehaviour))]
    public class DefaultLuaScriptSource : ILuaScriptSource
    {
        #region ILuaScriptSource Members

        public IEnumerable<KeyValuePair<string, string>> GetMenuItems()
        {
            return mItems;
        }

        #endregion

        public DefaultLuaScriptSource()
        {
            var luaRoot = ProjectPath.Get("Lua");
            mItems.Clear();
            mItems.AddRange(getItems(luaRoot, "UI"));
            mItems.AddRange(getItems(luaRoot, "Framework/Controls"));
        }

        private IEnumerable<KeyValuePair<string, string>> getItems(string rootPath, string dir)
        {
            var fullPath = Path.Combine(rootPath, dir);
            if (!Directory.Exists(fullPath))
                yield break;
            foreach (var file in Directory.GetFiles(fullPath, "*.lua", SearchOption.AllDirectories))
            {
                var item = PathUtility.FormatSeperator(Path.ChangeExtension(file.Substring(rootPath.Length + 1), null));
                yield return new KeyValuePair<string, string>("Root/" + item, item);
            }
        }

        private readonly List<KeyValuePair<string, string>> mItems = new List<KeyValuePair<string, string>>();
    }
}